import { defineStore } from 'pinia';
import http from '../common/request'

export default defineStore('userStore', {
	state: () => {
		return {
			userInfo: {},
			money: {}
		}
	},
	actions: {
		setUserInfo(userInfo) {
			this.userInfo = userInfo;
		},
		setMoney(res) {
			console.log(res,'----16')
			if(Array.isArray(res)){
				this.money={
					jnb: res.find((i)=>i.moneyType=='jnb'),
					usdt: res.find((i)=>i.moneyType=='usdt'),
					balance: res.find((i)=>i.moneyType=='balance'),
				}
			}else if(res.moneyType){
				this.money[res.moneyType]=res
			}
		},
		clear() {
			this.userInfo = {};
		},
		loadUser() {
			return new Promise((resolve, reject) => {
				http({
					url: '/user/self',
					method: 'GET'
				}).then((userInfo) => {
					this.setUserInfo(userInfo);
					resolve();
				}).catch((res) => {
					reject(res);
				});
			})
		},
		loadMoney(context){
			return new Promise((resolve, reject) => {
				http({
					url: `/user/money/${this.userInfo.id}`,
					method: "GET",
				}).then((res) => {
					if(res && res.length>0){
						this.setMoney(res);
						resolve(res);
					}
				}).catch((res) => {
					reject(res);
				});
			})
		},
	}
})